//package com.cloudpivot.project.rabbitmq.service;
//
//import com.cloudpivot.project.rabbitmq.util.ProjectUtil;
//import com.rabbitmq.client.*;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//
//@Component
//public class MayCurDelayConsumerService {
//    @Value("${rabbitmq-config.host-name}")
//    private String HOST_NAME;
//    @Value("${rabbitmq-config.host-port}")
//    private Integer HOST_PORT;
//    @Value("${rabbitmq-config.username}")
//    private String USER_NAME;
//    @Value("${rabbitmq-config.password}")
//    private String PASSWORD;
////    private static final String QUEUE_NAME = "first_queues";
//    private static final String MAYCUR_EXCHANGE_NAME = "maycur_exchange";
//    private static final String MAYCUR_QUEUE_NAME_1="normal_queue_1";
//    private static final String MAYCUR_QUEUE_NAME_2="normal_queue_2";
//    private static final String MAYCUR_QUEUE_NAME_3="normal_queue_3";
//    private static final String MAYCUR_QUEUE_NAME_FINAL="delay_queue_final";
//    private static final String MAY_CUR_VIRTUAL_HOST = "/maycur_opdca";
//
//
//
//
//    /**
//     * 每刻模拟延迟消费情况
//     */
//    @Bean
//    public void maycurDelayCunsumer() {
//        try {
//            ConnectionFactory factory=new ConnectionFactory();
//            factory.setHost(HOST_NAME);
//            factory.setPort(HOST_PORT);
//            factory.setUsername(USER_NAME);
//            factory.setPassword(PASSWORD);
//            factory.setVirtualHost(MAY_CUR_VIRTUAL_HOST);
//            Connection rabbitMQConnection= factory.newConnection();
//            Channel channel = rabbitMQConnection.createChannel();
//            Map<String, Object> paramMap = new HashMap<>();
//            paramMap.put("x-dead-letter-exchange", "delay_exchange");
//            paramMap.put("x-dead-letter-routing-key", "delay_key_1");
//            channel.queueDeclare(MAYCUR_QUEUE_NAME_1,true,false,false,paramMap);
//            channel.basicQos(1);
//            Consumer myConsumer=new DefaultConsumer(channel){
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    String routingKey = envelope.getRoutingKey();
//
//                    String contentType = properties.getContentType();
//                    String deliveryTag = String.valueOf(envelope.getDeliveryTag());
//                    String message=new String(body,"UTF-8");
//                    System.out.println("========首次每刻OPDCA正常消费者等待===============" + ProjectUtil.getNowTime());
//                    System.out.println("每刻OPDCA获得的消息为(1)=====>>" + message);
//                    System.out.println("每刻OPDCA该消息对应的参数(1)routingKey:"+routingKey+" deliveryTag:"+deliveryTag+" contentType:"+contentType);
////                    channel.basicAck(envelope.getDeliveryTag(),false);
//                    //tag对应的消息标识+拒绝当前消息+放入死信队列
//                    channel.basicNack(envelope.getDeliveryTag(),false,false);
//                }
//            };
//            channel.basicConsume(MAYCUR_QUEUE_NAME_1, false, myConsumer);
//        } catch (Exception e){
//            System.out.println("运行异常");
//            throw new RuntimeException(e);
//        }
//    }
//
//    /**
//     * 每刻模拟延迟消费情况
//     */
//    @Bean
//    public void maycurDelayCunsumer2() {
//        try {
//            ConnectionFactory factory=new ConnectionFactory();
//            factory.setHost(HOST_NAME);
//            factory.setPort(HOST_PORT);
//            factory.setUsername(USER_NAME);
//            factory.setPassword(PASSWORD);
//            factory.setVirtualHost(MAY_CUR_VIRTUAL_HOST);
//            Connection rabbitMQConnection= factory.newConnection();
//            Channel channel = rabbitMQConnection.createChannel();
//            Map<String, Object> paramMap = new HashMap<>();
//            paramMap.put("x-dead-letter-exchange", "delay_exchange");
//            paramMap.put("x-dead-letter-routing-key", "delay_key_2");
//            channel.queueDeclare(MAYCUR_QUEUE_NAME_2,true,false,false,paramMap);
//            channel.basicQos(1);
//            Consumer myConsumer=new DefaultConsumer(channel){
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    String routingKey = envelope.getRoutingKey();
//                    String contentType = properties.getContentType();
//                    String deliveryTag = String.valueOf(envelope.getDeliveryTag());
//                    String message=new String(body,"UTF-8");
//                    System.out.println("========二次每刻OPDCA正常消费者等待===============" + ProjectUtil.getNowTime());
//                    System.out.println("每刻OPDCA获得的消息为(1)=====>>" + message);
//                    System.out.println("每刻OPDCA该消息对应的参数(1)routingKey:"+routingKey+" deliveryTag:"+deliveryTag+" contentType:"+contentType);
////                    channel.basicAck(envelope.getDeliveryTag(),false);
//                    //tag对应的消息标识+拒绝当前消息+放入死信队列
//                    channel.basicNack(envelope.getDeliveryTag(),false,false);
//                }
//            };
//            channel.basicConsume(MAYCUR_QUEUE_NAME_2, false, myConsumer);
//        } catch (Exception e){
//            System.out.println("运行异常");
//            throw new RuntimeException(e);
//        }
//    }
//
//    /**
//     * 每刻模拟延迟消费情况
//     */
//    @Bean
//    public void maycurDelayCunsumer3() {
//        try {
//            ConnectionFactory factory=new ConnectionFactory();
//            factory.setHost(HOST_NAME);
//            factory.setPort(HOST_PORT);
//            factory.setUsername(USER_NAME);
//            factory.setPassword(PASSWORD);
//            factory.setVirtualHost(MAY_CUR_VIRTUAL_HOST);
//            Connection rabbitMQConnection= factory.newConnection();
//            Channel channel = rabbitMQConnection.createChannel();
//            Map<String, Object> paramMap = new HashMap<>();
//            paramMap.put("x-dead-letter-exchange", "delay_exchange");
//            paramMap.put("x-dead-letter-routing-key", "delay_key_final");
//            channel.queueDeclare(MAYCUR_QUEUE_NAME_3,true,false,false,paramMap);
//            channel.basicQos(1);
//            Consumer myConsumer=new DefaultConsumer(channel){
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    String routingKey = envelope.getRoutingKey();
//                    String contentType = properties.getContentType();
//                    String deliveryTag = String.valueOf(envelope.getDeliveryTag());
//                    String message=new String(body,"UTF-8");
//                    System.out.println("==========三次每刻OPDCA正常消费者等待=============" + ProjectUtil.getNowTime());
//                    System.out.println("每刻OPDCA获得的消息为(1)=====>>" + message);
//                    System.out.println("每刻OPDCA该消息对应的参数(1)routingKey:"+routingKey+" deliveryTag:"+deliveryTag+" contentType:"+contentType);
////                    channel.basicAck(envelope.getDeliveryTag(),false);
//                    //tag对应的消息标识+拒绝当前消息+放入死信队列
//                    channel.basicNack(envelope.getDeliveryTag(),false,false);
//                }
//            };
//            channel.basicConsume(MAYCUR_QUEUE_NAME_3, false, myConsumer);
//        } catch (Exception e){
//            System.out.println("运行异常");
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Bean
//    public void maycurDelayCunsumerFinal() {
//        try {
//            ConnectionFactory factory=new ConnectionFactory();
//            factory.setHost(HOST_NAME);
//            factory.setPort(HOST_PORT);
//            factory.setUsername(USER_NAME);
//            factory.setPassword(PASSWORD);
//            factory.setVirtualHost(MAY_CUR_VIRTUAL_HOST);
//            Connection rabbitMQConnection= factory.newConnection();
//            Channel channel = rabbitMQConnection.createChannel();
//            Map<String, Object> paramMap = new HashMap<>();
////            paramMap.put("x-dead-letter-exchange", "delay_exchange");
////            paramMap.put("x-dead-letter-routing-key", "delay_key_final");
//            channel.queueDeclare(MAYCUR_QUEUE_NAME_FINAL,true,false,false,paramMap);
//            channel.basicQos(1);
//            Consumer myConsumer=new DefaultConsumer(channel){
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    String routingKey = envelope.getRoutingKey();
//                    String contentType = properties.getContentType();
//                    String deliveryTag = String.valueOf(envelope.getDeliveryTag());
//                    String message=new String(body,"UTF-8");
//                    System.out.println("==========最后处理掉每刻OPDCA正常消费者等待=============" + ProjectUtil.getNowTime());
//                    System.out.println("每刻OPDCA获得的消息为(1)=====>>" + message);
//                    System.out.println("每刻OPDCA该消息对应的参数(1)routingKey:"+routingKey+" deliveryTag:"+deliveryTag+" contentType:"+contentType);
//                    channel.basicAck(envelope.getDeliveryTag(),false);
//                    //tag对应的消息标识+拒绝当前消息+放入死信队列
////                    channel.basicNack(envelope.getDeliveryTag(),false,false);
//                }
//            };
//            channel.basicConsume(MAYCUR_QUEUE_NAME_FINAL, false, myConsumer);
//        } catch (Exception e){
//            System.out.println("运行异常");
//            throw new RuntimeException(e);
//        }
//    }
//
//}
